<?php
//后台部门管理

define('IN_MMOA', true);

require dirname(__FILE__).'/includes/init.php';


if ($_REQUEST['act'] == '')
{
	$sql = "SELECT * FROM " . $mmoa->table('departments') . " WHERE 1";
	$departments = $db->getAll($sql);
	
	$smarty->assign('departments', $departments);

	$smarty->assign('mm_show', 'd_list');

	clear_caches();
	$smarty->display('department.htm');
}
elseif ($_REQUEST['act'] == 'add')
{
	// 添加部门信息
	//echo 'ok';
	
	$sql = "SELECT id, e_name FROM " . $mmoa->table('employees') . " WHERE 1";
	$employee = $db->getAll($sql);
	$smarty->assign('employee', $employee);
	
	$smarty->assign('mm_show', 'd_add');
	
	clear_caches();
	$smarty->display('department.htm');
}
elseif ($_REQUEST['act'] == 'addu')
{
	// 更新部门表
	$d = array();
	// 基本信息
	$d['name'] = isset($_POST['name']) ? trim($_POST['name']) : '';
	$d['manager'] = isset($_POST['manager']) ? trim($_POST['manager']) : '';
	
	if ($d['name']){
		$sql = "INSERT INTO " . $GLOBALS['mmoa']->table('departments') . "( `d_name`, `d_manager`) 
				VALUES ('".$d['name']."', '".$d['manager']."')";
		if($db->query($sql)){
			echo '<meta charset="utf-8">';
			echo '添加成功';
		} else {
			echo '<meta charset="utf-8">';
			echo '添加失败';
		}
	} else {
		echo '<meta charset="utf-8">';
		echo '添加失败';
	}

}
elseif ($_REQUEST['act'] == 'mod')
{
	// 修改部门信息
	//echo 'ok';
	
	$sql = "SELECT id, e_name FROM " . $mmoa->table('employees') . " WHERE 1";
	$employee = $db->getAll($sql);
	$smarty->assign('employee', $employee);
	
	$did = $_GET['did'];
	$sql = "SELECT * FROM " . $mmoa->table('departments') . " WHERE id = $did";
	$d = $db->getRow($sql);
	
	$smarty->assign('d', $d);
	$smarty->assign('did', $did);
	$smarty->assign('mm_show', 'd_mod');
	
	clear_caches();
	$smarty->display('department.htm');
}
elseif ($_REQUEST['act'] == 'modu')
{
	// 修改部门信息
	//echo 'ok';
	
	$d = array();
	// 基本信息
	$d['name'] = isset($_POST['name']) ? trim($_POST['name']) : '';
	$d['manager'] = isset($_POST['manager']) ? trim($_POST['manager']) : '';
	
	$did = $_POST['did'];
	
	if ($d['name']){
		$sql = "UPDATE " . $GLOBALS['mmoa']->table('departments') . " SET `d_name` = '".$d['name']."', `d_manager` = '".$d['manager']."' WHERE id = $did ";
		if($db->query($sql)){
			echo '<meta charset="utf-8">';
			echo '修改成功';
		} else {
			echo '<meta charset="utf-8">';
			echo '修改失败';
		}
	} else {
		echo '<meta charset="utf-8">';
		echo '修改失败';
	}
}
elseif ($_REQUEST['act'] == 'del')
{
	// 删除部门信息
	//echo 'ok';
	
	if ($_GET['did']){
		$did = $_GET['did'];
		$sql = "DELETE FROM " . $mmoa->table('departments') . " WHERE id=$did";
		if($db->query($sql)){
			echo '<meta charset="utf-8">';
			echo '删除成功';
		} else {
			echo '<meta charset="utf-8">';
			echo '删除失败';
		}
	}
	
}

?>